using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;



namespace SkinableFormsLib
{
	public class LWTextBoxCustom : LWTextBox
	{

		internal bool _hasShadow = false;

		internal Point _lwShadowShift;
		public Point LWShadowShift
		{
			get
			{
				return _lwShadowShift;
			}
			set
			{
				_lwShadowShift = value;
				_hasShadow = (!_lwShadowShift.IsEmpty && _lwShadowColor != Color.Transparent);
			}
		}

		private byte _lwSC_A, _lwSC_R, _lwSC_G, _lwSC_B;

		private Color _lwShadowColor = Color.Transparent;
		public Color LWShadowColor
		{
			get
			{
				return _lwShadowColor;
			}
			set
			{
				_lwShadowColor = value;
				_lwSC_A = _lwShadowColor.A;
				_lwSC_R = _lwShadowColor.R;
				_lwSC_G = _lwShadowColor.G;
				_lwSC_B = _lwShadowColor.B;
				_hasShadow = (!_lwShadowShift.IsEmpty && _lwShadowColor != Color.Transparent);

			}
		}


		protected override void OnCreateControl()
		{
			base.OnCreateControl();
			this.Select(0, 0);

		}

		private void CreateBitmap()
		{
			if (Width != 0 && Height != 0)
			{
				_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
				_lwRectangle = new Rectangle(0, 0, _lwBitmap.Width, _lwBitmap.Height);

			}
		}


		private void DrawShadow()
		{
			Bitmap _b = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);
			base.DrawToBitmap(_b, _lwRectangle);
			Utils.BitmapMakeTransparent(ref _b, ref _lwRectangle, ref  bc, ref gc, ref  rc);

			Bitmap _b2 = (Bitmap)_b.Clone();
			Utils.BitmapSetChannelsValue(ref _b, ref _lwRectangle, ref _lwSC_R, ref _lwSC_G, ref _lwSC_B);
			Utils.BitmapSetOpacity(ref _b, ref _lwRectangle, ref _lwSC_A);

			Graphics g = Graphics.FromImage(_lwBitmap);

			Rectangle rr = _lwRectangle;
			rr.Offset(_lwShadowShift);
			Utils.TextBoxAdjustShadowRectangle(Handle, ref rr);
			g.DrawImageUnscaledAndClipped(_b, rr);
			g.DrawImage(_b2, 0, 0);

			_b.Dispose();
			_b2.Dispose();
		}
		private void DrawText()
		{
			//ForeColor = cc;
			//this.DrawToBitmap(_lwBitmap, _lwRectangle);
		}

		protected override void WndProc(ref Message m)
		{

			base.WndProc(ref m);

			//if (m.Msg == Win32.WM_ERASEBKGND)//optional - needs more testing
			if (m.Msg == Win32.WM_PAINT)
			{
				if (!DesignMode)
				{
					if (_lwBitmap == null)
					{
						OnResize(null);
					}


					_lwTransparency = Utils.GetTransparency(this);
					OnResize(null);



					CreateBitmap();

					if (_hasShadow)
					{
						DrawShadow();
					}
					else
					{
						this.DrawToBitmap(_lwBitmap, _lwRectangle);
					}


					//_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
					if (_lwTransparency)
					{
						Utils.BitmapMakeTransparent(ref _lwBitmap, ref _lwRectangle, ref  bc, ref gc, ref  rc);
						//or
						//Utils.BitmapMakeTransparentUnsafe(ref _lwBitmap, ref _lwRectangle, ref  bc, ref gc, ref  rc); //I suspect this one is faster

					}
				}
			}


		}
	}
}
